package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.DeptDao;
import com.aaa.vegetablemarketexpress.entity.Dept;
import com.aaa.vegetablemarketexpress.entity.User;
import com.aaa.vegetablemarketexpress.service.DeptService;
import com.aaa.vegetablemarketexpress.util.CustomException;
import com.aaa.vegetablemarketexpress.util.ResultStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
/**
 * @FileName: DeptServiceImpl
 * @Description:
 * @Author: qxh
 * @CreateTime: 2025/9/9 14:22
 * @Version: 1.0.0
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    private HttpSession session;

    @Resource
    private DeptDao deptDao;

    /**
     * 根据ID查询部门
     * @param deptId
     * @return
     */
    @Override
    public Dept selectDeptById(Long deptId) {
        return deptDao.selectDeptById(deptId);
    }

    /**
     * 查询部门列表
     * @param dept
     * @return
     */
    @Override
    public List<Dept> selectDeptList(Dept dept) {
        return deptDao.selectDeptList(dept);
    }

    /**
     * 插入部门
     * @param dept
     * @return
     */
    @Override
    public int insertDept(Dept dept) {
        dept.setCreateTime(new Date());
        dept.setDelFlag("0");

        // 检查部门名称是否唯一
        if (!"0".equals(checkDeptNameUnique(dept))) {
            throw new CustomException(ResultStatus.FAILED.getStatusCode(), "部门名称已存在");
        }

        User currentUser = (User) session.getAttribute("userInfo");
        if (currentUser != null) {
            dept.setCreateBy(currentUser.getUserName());
        }
        dept.setCreateTime(new Date());
        return deptDao.insertDept(dept);
    }

    /**
     * 更新部门
     * @param dept
     * @return
     */
    @Override
    public int updateDept(Dept dept) {
        // 检查部门名称是否唯一
        if (!"0".equals(checkDeptNameUnique(dept))) {
            throw new CustomException(ResultStatus.FAILED.getStatusCode(), "部门名称已存在");
        }

        User currentUser = (User) session.getAttribute("userInfo");
        if (currentUser != null) {
            dept.setUpdateBy(currentUser.getUserName());
        }
        dept.setUpdateTime(new Date());
        return deptDao.updateDept(dept);
    }

    /**
     * 删除部门
     * @param deptId
     * @return
     */
    @Override
    public int deleteDeptById(Long deptId) {
        // 检查部门是否被使用
        if (deptDao.checkDeptUse(deptId) > 0) {
            throw new CustomException(ResultStatus.FAILED.getStatusCode(), "部门已被使用，不能删除");
        }
        return deptDao.deleteDeptById(deptId);
    }

    /**
     * 批量删除部门
     * @param deptIds
     * @return
     */
    @Override
    public int deleteDeptByIds(Long[] deptIds) {
        for (Long deptId : deptIds) {
            // 检查部门是否被使用
            if (deptDao.checkDeptUse(deptId) > 0) {
                throw new CustomException(ResultStatus.FAILED.getStatusCode(),
                        "部门ID " + deptId + " 已被使用，不能删除");
            }
        }
        return deptDao.deleteDeptByIds(deptIds);
    }

    /**
     * 检查部门名称是否唯一
     * @param dept
     * @return
     */
    @Override
    public String checkDeptNameUnique(Dept dept) {
        Dept info = deptDao.selectDeptByName(dept.getDeptName());
        if (info != null && !info.getDeptId().equals(dept.getDeptId())) {
            return "1"; // 已存在
        }
        return "0"; // 不存在
    }

    /**
     * 更新部门状态
     * @param deptId
     * @param status
     * @return
     */
    @Override
    @Transactional
    public int updateDeptStatus(Long deptId, String status) {
        return deptDao.updateDeptStatus(deptId, status);
    }

    /**
     * 查询所有正常部门
     * @return
     */
    @Override
    public List<Dept> selectAllNormalDept() {
        return deptDao.selectAllNormalDept();
    }

    /**
     * 查询部门总数
     * @param dept
     * @return
     */
    @Override
    public int countDept(Dept dept) {
        return deptDao.countDept(dept);
    }

    /**
     * 查询所有子部门
     * @param deptId
     * @return
     */
    @Override
    public List<Dept> selectChildrenDeptById(Long deptId) {
        return deptDao.selectChildrenDeptById(deptId);
    }
}